from app import create_app
from models import User, db
from utils import hash_password, verify_password

def check_admin():
    app = create_app()
    
    with app.app_context():
        # 检查admin用户是否存在
        admin = User.query.filter_by(username='admin').first()
        
        if not admin:
            print("admin用户不存在，正在创建...")
            admin = User(
                username='admin',
                password=hash_password('admin123'),
                role='admin'
            )
            db.session.add(admin)
            db.session.commit()
            print("admin用户已创建: admin/admin123")
        else:
            print(f"admin用户已存在，ID: {admin.id}")
            print(f"当前密码哈希: {admin.password}")
            
            # 测试密码
            test_password = 'admin123'
            if verify_password(test_password, admin.password):
                print(f"密码 '{test_password}' 验证成功")
            else:
                print(f"密码 '{test_password}' 验证失败")
                
                # 更新密码
                print("正在更新密码为 'admin123'...")
                admin.password = hash_password('admin123')
                db.session.commit()
                print("密码已更新")
        
        # 检查其他用户
        users = User.query.all()
        print(f"\n当前所有用户:")
        for user in users:
            print(f"- {user.username} (角色: {user.role})")

if __name__ == '__main__':
    check_admin()
